package com.freeze;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.apache.commons.codec.binary.Base64;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class PaddleHubClient {

    public static void main(String[] args) {
        String serverUrl = "http://172.16.12.229:8868/predict/ocr_system"; // PaddleHub Serving默认URL
        File imageFile = new File("E:\\1\\ppocr_img\\ppocr_img\\imgs\\11.jpg");

        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpPost uploadFile = new HttpPost(serverUrl);

            // Read image file and encode to Base64
            FileInputStream fileInputStream = new FileInputStream(imageFile);
            byte[] imageBytes = new byte[(int) imageFile.length()];
            fileInputStream.read(imageBytes);
            String encodedImage = Base64.encodeBase64String(imageBytes);
            fileInputStream.close();

            // Create JSON object with the Base64 encoded image
            JSONObject json = new JSONObject();
            json.put("images", new String[]{encodedImage});

            // Set the request content type to application/json
            StringEntity requestEntity = new StringEntity(json.toString(), "UTF-8");
            uploadFile.setEntity(requestEntity);
            uploadFile.setHeader("Content-Type", "application/json");

            // Execute the request
            CloseableHttpResponse response = httpClient.execute(uploadFile);
            HttpEntity responseEntity = response.getEntity();
            String responseString = EntityUtils.toString(responseEntity, "UTF-8");
            response.close();

            // Print the response
            System.out.println("Original Response:");
            System.out.println(responseString);

            // Parse the response and decode the text
            JSONObject jsonResponse = new JSONObject(responseString);
            if (jsonResponse.has("results")) {
                JSONArray resultsArray = jsonResponse.getJSONArray("results");
                for (int i = 0; i < resultsArray.length(); i++) {
                    JSONArray textRegions = resultsArray.getJSONArray(i);
                    for (int j = 0; j < textRegions.length(); j++) {
                        JSONObject textRegion = textRegions.getJSONObject(j);
                        String text = textRegion.getString("text");
                        String decodedText = StringEscapeUtils.unescapeJava(text);
                        System.out.println("Decoded Text: " + decodedText);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
